rm(list = ls())
require(stargazer)
require(zTree)

require(foreign)
require(stats4)

require(mfx)


#Load in some Data
zTT1= zTreeTables("C:/Users/naten/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/December5compression.xls")
subjects1=zTT1$subjects
subjects1$Subject=subjects1$Subject+100


#Load in some Data
zTT2= zTreeTables("C:/Users/naten/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/December11compression.xls")
subjects2=zTT2$subjects
subjects2$Subject=subjects2$Subject+200

subjects=rbind(subjects1,subjects2)

#Clean data
subjects$responder=(subjects$"response1[50]">0)
subjects=subset(subjects,responder>0)

uidlist=unique(subjects$Subject)

#find N
#**
length(uidlist)
#**


#create a frame with observations
obsframe=data.frame(uid=vector('numeric'),truenum=vector('numeric'),stnum=vector('numeric'),block=vector('numeric'), number=vector('numeric'), state=vector('numeric'), response=vector('numeric'))
for(i in 1:length(uidlist)){
playframe=subjects[i,]
uidplace=uidlist[i]

#block 1
for (j in 1:50){
blockplace=1
stnumplace=playframe[1,25]
truenumplace=playframe[1,(748+j)]
numberplace=j
stateplace=playframe[1,(998+j)]
responseplace=playframe[1,(84+j)]
addframe=data.frame(uid=uidplace,truenum=truenumplace,stnum=stnumplace,block=blockplace, number=numberplace, state=stateplace, response=responseplace)
obsframe=rbind(obsframe,addframe)
}

#block 2
for (j in 1:50){
blockplace=2
stnumplace=playframe[1,26]
truenumplace=playframe[1,(798+j)]
numberplace=j
stateplace=playframe[1,(1048+j)]
responseplace=playframe[1,(234+j)]
addframe=data.frame(uid=uidplace,truenum=truenumplace,stnum=stnumplace,block=blockplace, number=numberplace, state=stateplace, response=responseplace)
obsframe=rbind(obsframe,addframe)

}

}

obsframe$correct=(obsframe$state==obsframe$response)

#Correct fraction
sum(obsframe$correct)/length(obsframe$correct)

#reward value
r=10 


#Variables to be stored
acculin=matrix(rep(0,80),nrow=4,ncol=20)
accupow=matrix(rep(0,80),nrow=4,ncol=20)
accugen=matrix(rep(0,80),nrow=4,ncol=20)
accuexp=matrix(rep(0,80),nrow=4,ncol=20)
accunoneigh=matrix(rep(0,80),nrow=4,ncol=20)
dataaccu=matrix(rep(0,80),nrow=4,ncol=20)



#number of states used by different treatments
Nvec=c(2,6)

#States for different treatments
states=list()
states[[1]]=c(4:5)
states[[2]]=c(2:7)

correct=list()
incorrect=list()
accu=list()

#{*} Make correct vecs (needs changing between treatment types)
for(i in 1:2){
correct[[i]]=vector('numeric')
incorrect[[i]]=vector('numeric')
accu[[i]]=vector('numeric')
playframe=subset(obsframe, stnum==Nvec[i])
for(j in 1:Nvec[i]){
correct[[i]][j]=sum(playframe$correct*(obsframe$truenum==states[[i]][j]))
incorrect[[i]][j]=sum((1-playframe$correct)*(obsframe$truenum==states[[i]][j]))
accu[[i]][j]=sum(playframe$correct*(obsframe$truenum==states[[i]][j]))/sum(obsframe$truenum==states[[i]][j])
}
}

#regress performance on distance
obsframe$distance=abs(obsframe$truenum-4.5)
twostateframe=subset(obsframe)
sixstateframe=subset(obsframe)

#nothing to this
#model2state=logitmfx(correct~distance,data=twostateframe, robust=TRUE, clustervar1 = "uid")
#model2state 
model6state=logitmfx(correct~distance,data=sixstateframe, robust=TRUE, clustervar1 = "uid") 
model6state

#******************************************
stargazer(model6state$mfxest)
#******************************************

#Bar graph
dispmat=rbind(acculin[1,7:14],accupow[1,7:14],accuexp[1,7:14],accugen[1,7:14],accunoneigh[1,7:14], dataaccu[1,7:14])
#*****************************************
barplot(accu[[2]], beside=TRUE, main="Accuracy By State", xlab="True State ", ylab="Accuracy ", ylim=c(0.8,1),names.arg=c(2:7),xpd = FALSE)

#barplot(accu[[1]], beside=TRUE, main="Accuracy By State", xlab="True State ", ylab="Accuracy ", ylim=c(0.8,1),names.arg=c(4:5),xpd = FALSE)
